<?php
namespace app\api\controller;
use app\common\BaseController;
use app\api\model\InsurePersonal as InsurePersonalModel;
use app\api\model\InsureTeam as InsureTeamModel;
class Upload extends BaseController{
    public function __construct(){
        parent::__construct();
    }
    
    public function personal(){
        $file = request()->file('file');
        if($file){
            $path = ROOT_PATH . 'public' . DS . 'uploads';
            $info = $file->move($path);
            if($info){                
                // 成功上传后 获取上传信息
                include(EXTEND_PATH.'phpexcel/PHPExcel/Reader/Excel2007.php'); 
                $epath = $path."/".$info->getSaveName();
                $phpReader = new \PHPExcel_Reader_Excel2007();  
                $phpExcel = $phpReader->load($epath);
                $curSheet = $phpExcel->getSheet(0);
                $allRow = $curSheet->getHighestRow();
                $allColumn = $curSheet->getHighestColumn();    
                $count=0;  
                $list=[];          
                for ($j = 2; $j <= $allRow; $j++) {
                    // 读取单元格
                    $param=[
                        'insure_code'=>(string)$phpExcel->getActiveSheet()->getCell("A".$j)->getValue(),
                        'doc_no'=>(string)$phpExcel->getActiveSheet()->getCell("B".$j)->getValue(),
                        'insure_no'=>(string)$phpExcel->getActiveSheet()->getCell("C".$j)->getValue(),
                        'policy_no'=>(string)$phpExcel->getActiveSheet()->getCell("D".$j)->getValue(),
                        'insure_name'=>(string)$phpExcel->getActiveSheet()->getCell("F".$j)->getValue(),
                        'insure_id'=>(string)$phpExcel->getActiveSheet()->getCell("G".$j)->getValue(),
                        'policy_holder'=>(string)$phpExcel->getActiveSheet()->getCell("AT".$j)->getValue(),
                        'policy_holder_id'=>(string)$phpExcel->getActiveSheet()->getCell("AU".$j)->getValue(),
                        'start_date'=>gmdate('Y-m-d',\PHPExcel_Shared_Date::ExcelToPHP($phpExcel->getActiveSheet()->getCell("I".$j)->getValue())),
                        'end_date'=>gmdate('Y-m-d',\PHPExcel_Shared_Date::ExcelToPHP($phpExcel->getActiveSheet()->getCell("J".$j)->getValue())),
                        'premium'=>(string)$phpExcel->getActiveSheet()->getCell("H".$j)->getValue(),
                        'insurance_date'=>gmdate('Y-m-d H:i:s',\PHPExcel_Shared_Date::ExcelToPHP($phpExcel->getActiveSheet()->getCell("K".$j)->getValue())),
                        'salesman_no'=>(string)$phpExcel->getActiveSheet()->getCell("L".$j)->getValue(),
                        'operator_no'=>(string)$phpExcel->getActiveSheet()->getCell("M".$j)->getValue(),
                        'insure_status'=>(string)$phpExcel->getActiveSheet()->getCell("N".$j)->getValue()
                    ];   
                    $list[$count]=$param;           
                    $count++;
                }
                $model = new InsurePersonalModel();
                $data = $model->saveAll($list);
                echo $count;
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }            
        }
    }
  

    public function team(){
        $file = request()->file('file');
        if($file){
            $path = ROOT_PATH . 'public' . DS . 'uploads';
            $info = $file->move($path);
            if($info){
                // 成功上传后 获取上传信息
                include(EXTEND_PATH.'phpexcel/PHPExcel/Reader/Excel2007.php'); 
                $epath = $path."/".$info->getSaveName();
                $phpReader = new \PHPExcel_Reader_Excel2007();  
                $phpExcel = $phpReader->load($epath);
                $curSheet = $phpExcel->getSheet(0);
                $allRow = $curSheet->getHighestRow();
                $allColumn = $curSheet->getHighestColumn();    
                $count=0;  
                $list=[];            
                for ($j = 2; $j <= $allRow; $j++) {
                    // 读取单元格
                    $param=[
                        'voucher_no'=>(string)$phpExcel->getActiveSheet()->getCell("B".$j)->getValue(),
                        'team_no'=>(string)$phpExcel->getActiveSheet()->getCell("C".$j)->getValue(),
                        'sn'=>(string)$phpExcel->getActiveSheet()->getCell("A".$j)->getValue(),
                        'insure_name'=>(string)$phpExcel->getActiveSheet()->getCell("D".$j)->getValue(),
                        'insure_id'=>(string)$phpExcel->getActiveSheet()->getCell("E".$j)->getValue(),
                        'insure_code'=>(string)$phpExcel->getActiveSheet()->getCell("F".$j)->getValue(),
                        'product_name'=>(string)$phpExcel->getActiveSheet()->getCell("G".$j)->getValue(),
                        'insurance_date'=>gmdate('Y-m-d H:i:s',\PHPExcel_Shared_Date::ExcelToPHP($phpExcel->getActiveSheet()->getCell("H".$j)->getValue())),
                        'start_date'=>gmdate('Y-m-d',\PHPExcel_Shared_Date::ExcelToPHP($phpExcel->getActiveSheet()->getCell("I".$j)->getValue())),
                        'end_date'=>gmdate('Y-m-d',\PHPExcel_Shared_Date::ExcelToPHP($phpExcel->getActiveSheet()->getCell("J".$j)->getValue())),
                        'policy_holder'=>(string)$phpExcel->getActiveSheet()->getCell("K".$j)->getValue(),
                        'premium'=>(string)$phpExcel->getActiveSheet()->getCell("L".$j)->getValue(),
                        'organization_name'=>(string)$phpExcel->getActiveSheet()->getCell("S".$j)->getValue(),
                        'salesman_no'=>(string)$phpExcel->getActiveSheet()->getCell("O".$j)->getValue(),
                        'operator_no'=>(string)$phpExcel->getActiveSheet()->getCell("N".$j)->getValue(),
                        'line'=>(string)$phpExcel->getActiveSheet()->getCell("R".$j)->getValue(),
                        'insure_status'=>(string)$phpExcel->getActiveSheet()->getCell("P".$j)->getValue()
                    ];
                    $list[$count]=$param;           
                    $count++;
                }
                $model = new InsureTeamModel();
                $data = $model->saveAll($list);
                echo $count;
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }            
        }
    }
}
